#include "Python.h"
#include <Ph.h>
#include <Pt.h>
#define PX_IMAGE_MODULES
#define PX_GIF_SUPPORT
#define PX_BMP_SUPPORT
#include <photon/PxImage.h>
//#include <photon/PxProto.h>


typedef struct {
	PyObject_HEAD
	PtWidget_t	*PtWidget;
} PtObject;

static PyTypeObject PtObject_Type;

/* PtObject methods */

static PtObject *
create(PtObject *self, PyObject *args)
{
PtObject 	*Widget;
PyObject	*WidgetClass;

	PyArg_ParseTuple(args,"O", &WidgetClass);
	Widget = PyObject_New(PtObject, &PtObject_Type);
	Widget->PtWidget = PtCreateWidget(PyCObject_AsVoidPtr(WidgetClass), self->PtWidget, 0, NULL);
	return Widget;
}

int
Callback(PtWidget_t *widget, void *data, PtCallbackInfo_t *info)
{
	if (PyCallable_Check((PyObject *)data)) {
		PyObject_CallObject((PyObject *)data, NULL);
	}
	return Pt_CONTINUE;
}

static PyObject *
realize(PtObject *self)
{
	PtRealizeWidget(self->PtWidget);
	Py_RETURN_NONE;
}

static PyObject *
getresource(PtObject *self, PyObject *args)
{
PtArg_t 	PtArgs[1];
PyObject	*Pyvar;

long		type;
PhArea_t	*area;
PhDim_t		*dim;
PhRgb_t		*color;
char		*str;
long		scalar;


	if (!PyArg_ParseTuple(args, "l", &type)) {
		Py_RETURN_NONE;
	}

	switch (type) {
	case Pt_ARG_FLAGS:
		PtSetArg( &PtArgs[0], type, scalar, 0);
		if (PtGetResources(self->PtWidget, 1, PtArgs)) {
			PyErr_Warn(PyExc_RuntimeWarning, "Can't get resource");
			Py_RETURN_NONE;
		}
		Pyvar = Py_BuildValue("l", scalar);
		break;
	case Pt_ARG_AREA:
		PtSetArg( &PtArgs[0], type, &area, 0);
		if (PtGetResources(self->PtWidget, 1, PtArgs)) {
			PyErr_Warn(PyExc_RuntimeWarning, "Can't get resource");
			Py_RETURN_NONE;
		}
		Pyvar = Py_BuildValue("llll", area->pos.x, area->pos.y, area->size.w, area->size.h);
		break;
	case Pt_ARG_WINDOW_TITLE:
	case Pt_ARG_TEXT_FONT:
	case Pt_ARG_TEXT_STRING:
	case Pt_ARG_HELP_TOPIC:
		PtSetArg( &PtArgs[0], type, &str, 0);
		if (PtGetResources(self->PtWidget, 1, PtArgs)) {
			PyErr_Warn(PyExc_RuntimeWarning, "Can't get resource");
			Py_RETURN_NONE;
		}
		Pyvar = Py_BuildValue("s", str);
		break;
	}
	return Pyvar;
}

static PyObject *
setresource(PtObject *self, PyObject *args)
{
PtArg_t		PtArgs[1];
PyObject	*Pytype;
PyObject	*Pyvar;

long		type;
PhArea_t	area;
PhDim_t		dim;
PhPoint_t	point;
PhRgb_t		color;
char		*str;
long		scalar;

	if (!PyArg_UnpackTuple(args, NULL, 2, 2, &Pytype, &Pyvar)) {
		Py_RETURN_NONE;
	}

	if (!PyArg_Parse(Pytype, "l", &type)) {
		Py_RETURN_NONE;
	}

	switch (type) {
	case Pt_ARG_FLAGS:
	case Pt_ARG_EFLAGS:
		if (!PyArg_Parse(Pyvar, "l", &scalar)) {
			Py_RETURN_NONE;
		}
		PtSetArg( &PtArgs[0], type, scalar, 0);
		break;
	case Pt_ARG_WINDOW_TITLE_COLOR:
	case Pt_ARG_CURSOR_COLOR:
		if (!PyArg_Parse(Pyvar, "bbb", &color.r, &color.g, &color.b)) {
			Py_RETURN_NONE;
		}
		PtSetArg( &PtArgs[0], type, PgRGB(color.r, color.g, color.b), 0);
		break;
	case Pt_ARG_BORDER_WIDTH:
	case Pt_ARG_MAX_HEIGHT:
	case Pt_ARG_MAX_WIDTH:
	case Pt_ARG_MIN_HEIGHT:
	case Pt_ARG_MIN_WIDTH:
		if (!PyArg_Parse(Pyvar, "l", &scalar)) {
			Py_RETURN_NONE;
		}
		PtSetArg( &PtArgs[0], type, scalar, 0);
		break;
	case Pt_ARG_DIM:
		if (!PyArg_ParseTuple(Pyvar, "hh", &dim.w, &dim.h)) {
			Py_RETURN_NONE;
		}
		PtSetArg( &PtArgs[0], type, &dim, 0);
		break;
	case Pt_ARG_POS:
		if (!PyArg_ParseTuple(Pyvar, "hh", &point.x, &point.y)) {
			Py_RETURN_NONE;
		}
		PtSetArg( &PtArgs[0], type, &point, 0);
		break;
	case Pt_ARG_AREA:
		if (!PyArg_ParseTuple(Pyvar, "hhhh", &area.pos.x, &area.pos.y, &area.size.w, &area.size.h)) {
			Py_RETURN_NONE;
		}
		PtSetArg( &PtArgs[0], type, &area, 0);
		break;
	case Pt_ARG_WINDOW_TITLE:
	case Pt_ARG_TEXT_FONT:
	case Pt_ARG_TEXT_STRING:
	case Pt_ARG_HELP_TOPIC:
		if (!PyArg_Parse(Pyvar, "s", &str)) {
			Py_RETURN_NONE;
		}
		PtSetArg( &PtArgs[0], type, str, 0);
		break;
	}
	if (PtSetResources(self->PtWidget, 1, PtArgs)) {
		PyErr_Warn(PyExc_RuntimeWarning, "Can't set resource");
	}
	Py_RETURN_NONE;
}

static PyObject *
addcallback(PtObject *self, PyObject *args)
{
PyObject	*PyCallback;

long		type;

	if (!PyArg_ParseTuple(args, "lO", &type, &PyCallback)) {
		Py_RETURN_NONE;
	}
	switch (type) {
	case Pt_CB_BLOCKED:
	case Pt_CB_DESTROYED:
	case Pt_CB_REALIZED:
	case Pt_CB_UNREALIZED:
	case Pt_CB_ACTIVATE:
	case Pt_CB_ARM:
	case Pt_CB_DISARM:
	case Pt_CB_GOT_FOCUS:
	case Pt_CB_LOST_FOCUS:
	case Pt_CB_MENU:
	case Pt_CB_REPEAT:
		PtAddCallback(self->PtWidget, type, Callback, PyCallback);
		break;
	case Pt_CB_RAW:
//		PtAddEventHandler(self->PtWidget, mask, Callback, PyCallback);
		break;
	case Pt_CB_HOTKEY:
//		PtAddHotkeyHandler(self->PtWidget, key_cap, mod, flags, Callback, PyCallback);
		break;
	}
	Py_RETURN_NONE;
}

static PyObject *
removecallback(PtObject *self, PyObject *args)
{
	Py_RETURN_NONE;
}

static void
Widget_dealloc(PtObject *self)
{
	PyObject_Del(self);
}

static PyMethodDef PtObject_methods[] = {
	{"Create", (PyCFunction)create, METH_VARARGS,
		PyDoc_STR("create new widget")},
	{"Set", (PyCFunction)setresource, METH_VARARGS,
		PyDoc_STR("set resource")},
	{"Get", (PyCFunction)getresource, METH_VARARGS,
		PyDoc_STR("get resource")},
	{"Add", (PyCFunction)addcallback, METH_VARARGS,
		PyDoc_STR("add callback")},
	{"Remove", (PyCFunction)removecallback, METH_VARARGS,
		PyDoc_STR("remove callback")},
	{"Realize",	(PyCFunction)realize, METH_NOARGS,
		PyDoc_STR("realizing widget")},
	{NULL}		/* sentinel */
};

static PyTypeObject PtObject_Type = {
    PyObject_HEAD_INIT(NULL)
    0,		/*ob_size*/
    "photon.Widget",		/*tp_name*/
    sizeof(PtObject),	/*tp_basicsize*/
    0,		/*tp_itemsize*/
    /* methods */
    (destructor)Widget_dealloc, /*tp_dealloc*/
    0,		/*tp_print*/
    0,		/*tp_getattr*/
    0,		/*tp_setattr*/
    0,		/*tp_compare*/
    0,		/*tp_repr*/
    0,		/*tp_as_number*/
    0,		/*tp_as_sequence*/
    0,		/*tp_as_mapping*/
    0,		/*tp_hash*/
    0,          /*tp_call*/
    0,          /*tp_str*/
    0,          /*tp_getattro*/
    0,          /*tp_setattro*/
    0,          /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT,     /*tp_flags*/
    0,          /*tp_doc*/
    0,          /*tp_traverse*/
    0,          /*tp_clear*/
    0,          /*tp_richcompare*/
    0,          /*tp_weaklistoffset*/
    0,          /*tp_iter*/
    0,          /*tp_iternext*/
    PtObject_methods,	/*tp_methods*/
    0,          /*tp_members*/
    0,		/*tp_getset*/
    0,          /*tp_base*/
    0,          /*tp_dict*/
    0,          /*tp_descr_get*/
    0,          /*tp_descr_set*/
    0,          /*tp_dictoffset*/
    0,          /*tp_init*/
    0,          /*tp_alloc*/
    0,          /*tp_new*/
    0,          /*tp_free*/
    0,          /*tp_is_gc*/
};
/* --------------------------------------------------------------------- */

static PyObject *
RegionQuery(PyObject *self, PyObject *args)
{
PhWindowInfo_t*    window_info;
PhRegion_t         region;
PhRect_t           rect;
PhRid_t            rid;
char               data[1000];

	if (!PyArg_ParseTuple(args, "l", &rid)) {
		Py_RETURN_NONE;
	}

	if ( !PhRegionQuery( rid, &region, &rect, (PhRegionDataHdr_t *)&data, sizeof(data) ) )
	{
		window_info = (PhWindowInfo_t *)PhRegionDataFindType( &region, (PhRegionDataHdr_t *)&data, Ph_RDATA_WINDOW);
//		if (window_info) {
//			window_info->title;
//		}
	}
	return Py_BuildValue("s", &window_info->title);
}

static PyObject *
QueryRids(PyObject *self, PyObject *args)
{
PyObject	*item;
PyObject	*tuple;
PhRid_t     rids[32*1024];
int         i, num_rids;

	num_rids = PhQueryRids(Ph_RIDQUERY_TOWARD,0,0,Ph_WINDOW_REGION,0,Ph_ROOT_RID,NULL,rids,100);
	tuple = PyTuple_New(num_rids);
	for(i=0;i<num_rids;i++) {
		PyTuple_SET_ITEM(tuple, i, Py_BuildValue("i", rids[i]));
	}
	return tuple;
}

static PyObject *
EventEmit(PyObject *self, PyObject *args)
{
PyObject	*Pyevent_type;
PyObject	*Pyrect;
PyObject	*Pykey_cap;

PhRect_t	rect;
PhEvent_t   event;
PhKeyEvent_t	key;
unsigned long	key_cap;

	if (!PyArg_UnpackTuple(args, NULL, 3, 3, &Pyevent_type, &Pyrect, &Pykey_cap)) {
		Py_RETURN_NONE;
	}

	memset(&event, 0, sizeof(event));

	if (!PyArg_Parse(Pyevent_type, "l", &event.type)) {
		Py_RETURN_NONE;
	}

//	rect.ul.x = rect.ul.y = SHRT_MIN;
//	rect.lr.x = rect.lr.y = SHRT_MAX;
	if (!PyArg_ParseTuple(Pyrect, "hhhh", &rect.ul.x, &rect.ul.y, &rect.lr.x, &rect.lr.y)) {
		Py_RETURN_NONE;
	}

	switch (event.type) {
	case Ph_EV_KEY:
		event.emitter.rid	= Ph_DEV_RID;
		event.collector.rid	= 0;
//		event.flags = Ph_EVENT_DIRECT;
		event.num_rects = 1;
		event.data_len = sizeof( key );
		memset(&key, 0, sizeof(key));
		if (!PyArg_Parse(Pykey_cap, "l", &key_cap)) {
		Py_RETURN_NONE;
		}
		key.key_sym = key.key_cap = key_cap;
		key.key_flags = Pk_KF_Sym_Valid | Pk_KF_Cap_Valid | Pk_KF_Key_Down;
		break;
	}

	if (PhEventEmit( &event, &rect, &key)) {
		PyErr_Warn(PyExc_RuntimeWarning, "Can't emit event");
	}


//	key.key_flags = Pk_KF_Sym_Valid | Pk_KF_Cap_Valid;
//	PhEventEmit( &event, &rect, &key);
	Py_RETURN_NONE;
}


static PtObject *
Attach(PyObject *self, PyObject *args)
{
        if (NULL == PhAttach(NULL, NULL)) {
            PyErr_Warn(PyExc_RuntimeWarning, "Can't attach Photon");
        }
        Py_RETURN_NONE;
}

static PtObject *
AppInit(PyObject *self, PyObject *args)
{
int 		argc;
char 		**argv;
PtObject	*Widget;

//	PyArg_ParseTuple(args, "s", &argv);
//	PySys_SetArgv(argc, argv);
        Widget = PyObject_New(PtObject, &PtObject_Type);
        Widget->PtWidget = PtAppInit(NULL, &argc, argv, 0, NULL);
        return Widget;
}

static PyObject *
MainLoop(PyObject *self, PyObject *args)
{
        PtMainLoop();
        Py_RETURN_NONE;
}


/* List of functions defined in the module */

static PyMethodDef photon_methods[] = {
	{"RegionQuery",	(PyCFunction)RegionQuery, METH_VARARGS,
		PyDoc_STR("region query")},
	{"QueryRids",	(PyCFunction)QueryRids, METH_NOARGS,
		PyDoc_STR("query rids")},
	{"EventEmit",	(PyCFunction)EventEmit, METH_VARARGS,
		PyDoc_STR("emit event")},
	{"Attach",	(PyCFunction)Attach, METH_NOARGS,
		PyDoc_STR("attch to photon channel")},
	{"AppInit",	(PyCFunction)AppInit, METH_NOARGS,
		PyDoc_STR("application init")},
	{"MainLoop",	(PyCFunction)MainLoop, METH_NOARGS,
		PyDoc_STR("main loop")},
	{NULL}		/* sentinel */
};

PyDoc_STRVAR(photon_doc, "This is a photon module.");

PyMODINIT_FUNC
initphoton(void)
{
    PyObject *m;

    /* Create the module and add the functions */
    m = Py_InitModule3("photon", photon_methods, photon_doc);
    if (m == NULL)
        return;

// EventType
    PyModule_AddIntConstant(m, "Ph_EV_KEY", Ph_EV_KEY);
// Keys (Pk_a..Pk_Z is ord('a')..ord('Z'))
    PyModule_AddIntConstant(m, "Pk_BackSpace", Pk_BackSpace);
    PyModule_AddIntConstant(m, "Pk_Tab", Pk_Tab);
    PyModule_AddIntConstant(m, "Pk_Linefeed", Pk_Linefeed);
    PyModule_AddIntConstant(m, "Pk_Clear", Pk_Clear);
    PyModule_AddIntConstant(m, "Pk_Return", Pk_Return);
    PyModule_AddIntConstant(m, "Pk_Pause", Pk_Pause);
    PyModule_AddIntConstant(m, "Pk_Scroll_Lock", Pk_Scroll_Lock);
    PyModule_AddIntConstant(m, "Pk_Escape", Pk_Escape);
    PyModule_AddIntConstant(m, "Pk_Delete", Pk_Delete);

    PyModule_AddIntConstant(m, "Pk_Home", Pk_Home);
    PyModule_AddIntConstant(m, "Pk_Left", Pk_Left);
    PyModule_AddIntConstant(m, "Pk_Up", Pk_Up);
    PyModule_AddIntConstant(m, "Pk_Right", Pk_Right);
    PyModule_AddIntConstant(m, "Pk_Down", Pk_Down);
    PyModule_AddIntConstant(m, "Pk_Prior", Pk_Prior);
    PyModule_AddIntConstant(m, "Pk_Pg_Up", Pk_Pg_Up);
    PyModule_AddIntConstant(m, "Pk_Next", Pk_Next);
    PyModule_AddIntConstant(m, "Pk_Pg_Down", Pk_Pg_Down);
    PyModule_AddIntConstant(m, "Pk_End", Pk_End);
    PyModule_AddIntConstant(m, "Pk_Begin", Pk_Begin);

    PyModule_AddIntConstant(m, "Pk_Select", Pk_Select);
    PyModule_AddIntConstant(m, "Pk_Print", Pk_Print);
    PyModule_AddIntConstant(m, "Pk_Execute", Pk_Execute);
    PyModule_AddIntConstant(m, "Pk_Insert", Pk_Insert);
    PyModule_AddIntConstant(m, "Pk_Undo", Pk_Undo);
    PyModule_AddIntConstant(m, "Pk_Redo", Pk_Redo);
    PyModule_AddIntConstant(m, "Pk_Menu", Pk_Menu);
    PyModule_AddIntConstant(m, "Pk_Find", Pk_Find);
    PyModule_AddIntConstant(m, "Pk_Cancel", Pk_Cancel);
    PyModule_AddIntConstant(m, "Pk_Help", Pk_Help);
    PyModule_AddIntConstant(m, "Pk_Break", Pk_Break);
    PyModule_AddIntConstant(m, "Pk_Num_Lock", Pk_Num_Lock);

    PyModule_AddIntConstant(m, "Pk_KP_Space", Pk_KP_Space);
    PyModule_AddIntConstant(m, "Pk_KP_Tab", Pk_KP_Tab);
    PyModule_AddIntConstant(m, "Pk_KP_Enter", Pk_KP_Enter);
    PyModule_AddIntConstant(m, "Pk_KP_F1", Pk_KP_F1);
    PyModule_AddIntConstant(m, "Pk_KP_F2", Pk_KP_F2);
    PyModule_AddIntConstant(m, "Pk_KP_F3", Pk_KP_F3);
    PyModule_AddIntConstant(m, "Pk_KP_F4", Pk_KP_F4);
    PyModule_AddIntConstant(m, "Pk_KP_Equal", Pk_KP_Equal);
    PyModule_AddIntConstant(m, "Pk_KP_Multiply", Pk_KP_Multiply);
    PyModule_AddIntConstant(m, "Pk_KP_Add", Pk_KP_Add);
    PyModule_AddIntConstant(m, "Pk_KP_Separator", Pk_KP_Separator);
    PyModule_AddIntConstant(m, "Pk_KP_Subtract", Pk_KP_Subtract);
    PyModule_AddIntConstant(m, "Pk_KP_Decimal", Pk_KP_Decimal);
    PyModule_AddIntConstant(m, "Pk_KP_Divide", Pk_KP_Divide);

    PyModule_AddIntConstant(m, "Pk_KP_0", Pk_KP_0);
    PyModule_AddIntConstant(m, "Pk_KP_1", Pk_KP_1);
    PyModule_AddIntConstant(m, "Pk_KP_2", Pk_KP_2);
    PyModule_AddIntConstant(m, "Pk_KP_3", Pk_KP_3);
    PyModule_AddIntConstant(m, "Pk_KP_4", Pk_KP_4);
    PyModule_AddIntConstant(m, "Pk_KP_5", Pk_KP_5);
    PyModule_AddIntConstant(m, "Pk_KP_6", Pk_KP_6);
    PyModule_AddIntConstant(m, "Pk_KP_7", Pk_KP_7);
    PyModule_AddIntConstant(m, "Pk_KP_8", Pk_KP_8);
    PyModule_AddIntConstant(m, "Pk_KP_9", Pk_KP_9);

    PyModule_AddIntConstant(m, "Pk_Shift_L", Pk_Shift_L);
    PyModule_AddIntConstant(m, "Pk_Shift_R", Pk_Shift_R);
    PyModule_AddIntConstant(m, "Pk_Control_L", Pk_Control_L);
    PyModule_AddIntConstant(m, "Pk_Control_R", Pk_Control_R);
    PyModule_AddIntConstant(m, "Pk_Caps_Lock", Pk_Caps_Lock);
    PyModule_AddIntConstant(m, "Pk_Shift_Lock", Pk_Shift_Lock);

    PyModule_AddIntConstant(m, "Pk_Meta_L", Pk_Meta_L);
    PyModule_AddIntConstant(m, "Pk_Meta_R", Pk_Meta_R);
    PyModule_AddIntConstant(m, "Pk_Alt_L", Pk_Alt_L);
    PyModule_AddIntConstant(m, "Pk_Alt_R", Pk_Alt_R);
    PyModule_AddIntConstant(m, "Pk_Super_L", Pk_Super_L);
    PyModule_AddIntConstant(m, "Pk_Super_R", Pk_Super_R);
    PyModule_AddIntConstant(m, "Pk_Hyper_L", Pk_Hyper_L);
    PyModule_AddIntConstant(m, "Pk_Hyper_R", Pk_Hyper_R);

    PyModule_AddIntConstant(m, "Pk_F1", Pk_F1);
    PyModule_AddIntConstant(m, "Pk_F2", Pk_F2);
    PyModule_AddIntConstant(m, "Pk_F3", Pk_F3);
    PyModule_AddIntConstant(m, "Pk_F4", Pk_F4);
    PyModule_AddIntConstant(m, "Pk_F5", Pk_F5);
    PyModule_AddIntConstant(m, "Pk_F6", Pk_F6);
    PyModule_AddIntConstant(m, "Pk_F7", Pk_F7);
    PyModule_AddIntConstant(m, "Pk_F8", Pk_F8);
    PyModule_AddIntConstant(m, "Pk_F9", Pk_F9);
    PyModule_AddIntConstant(m, "Pk_F10", Pk_F10);

// EFLAGS
    PyModule_AddIntConstant(m, "Pt_CONSUME_EVENTS", Pt_CONSUME_EVENTS);
    PyModule_AddIntConstant(m, "Pt_INTERNAL_HELP", Pt_INTERNAL_HELP);
    PyModule_AddIntConstant(m, "Pt_DAMAGE_PARENT", Pt_DAMAGE_PARENT);
// FLAGS
    PyModule_AddIntConstant(m, "Pt_CALLBACKS_ACTIVE", Pt_DAMAGE_PARENT);
// RESIZE_FLAGS
// CONTAINER_FLAGS
// GROUP_FLAGS
// LABEL_FLAGS

// Widget
    PyModule_AddObject(m, "PtWidget", PyCObject_FromVoidPtr(PtWidget, NULL));
    PyModule_AddIntConstant(m, "Pt_ARG_AREA", Pt_ARG_AREA);
    PyModule_AddIntConstant(m, "Pt_ARG_BITMAP_CURSOR", Pt_ARG_BITMAP_CURSOR);
    PyModule_AddIntConstant(m, "Pt_ARG_BORDER_WIDTH", Pt_ARG_BORDER_WIDTH);
    PyModule_AddIntConstant(m, "Pt_ARG_CURSOR_COLOR", Pt_ARG_CURSOR_COLOR);
    PyModule_AddIntConstant(m, "Pt_ARG_CURSOR_TYPE", Pt_ARG_CURSOR_TYPE);
    PyModule_AddIntConstant(m, "Pt_ARG_HELP_TOPIC", Pt_ARG_HELP_TOPIC);
    PyModule_AddIntConstant(m, "Pt_ARG_DATA", Pt_ARG_DATA);
    PyModule_AddIntConstant(m, "Pt_ARG_DIM", Pt_ARG_DIM);
    PyModule_AddIntConstant(m, "Pt_ARG_EFLAGS", Pt_ARG_EFLAGS);
    PyModule_AddIntConstant(m, "Pt_ARG_FLAGS", Pt_ARG_FLAGS);
    PyModule_AddIntConstant(m, "Pt_ARG_POS", Pt_ARG_POS);
    PyModule_AddIntConstant(m, "Pt_ARG_RESIZE_FLAGS", Pt_ARG_RESIZE_FLAGS);
    PyModule_AddIntConstant(m, "Pt_ARG_USER_DATA", Pt_ARG_USER_DATA);
// Widget -> Basic
    PyModule_AddObject(m, "PtBasic", PyCObject_FromVoidPtr(PtBasic, NULL));
    PyModule_AddIntConstant(m, "Pt_ARG_BANDWIDTH_THRESHOLD", Pt_ARG_BANDWIDTH_THRESHOLD);
    PyModule_AddIntConstant(m, "Pt_ARG_BOT_BORDER_COLOR", Pt_ARG_BOT_BORDER_COLOR);
    PyModule_AddIntConstant(m, "Pt_ARG_COLOR", Pt_ARG_COLOR);
    PyModule_AddIntConstant(m, "Pt_ARG_FILL_COLOR", Pt_ARG_FILL_COLOR);
    PyModule_AddIntConstant(m, "Pt_ARG_FILL_PATTERN", Pt_ARG_FILL_PATTERN);
    PyModule_AddIntConstant(m, "Pt_ARG_HIGHLIGHT_ROUNDNESS", Pt_ARG_HIGHLIGHT_ROUNDNESS);
    PyModule_AddIntConstant(m, "Pt_ARG_MARGIN_HEIGHT", Pt_ARG_MARGIN_HEIGHT);
    PyModule_AddIntConstant(m, "Pt_ARG_MARGIN_WIDTH", Pt_ARG_MARGIN_WIDTH);
    PyModule_AddIntConstant(m, "Pt_ARG_TOP_BORDER_COLOR", Pt_ARG_TOP_BORDER_COLOR);
    PyModule_AddIntConstant(m, "Pt_ARG_TRANS_PATTERN", Pt_ARG_TRANS_PATTERN);
    PyModule_AddIntConstant(m, "Pt_CB_ACTIVATE", Pt_CB_ACTIVATE);
// Widget -> Basic -> Bitmap
    PyModule_AddObject(m, "PtBitmap", PyCObject_FromVoidPtr(PtBitmap, NULL));
    PyModule_AddIntConstant(m, "Pt_ARG_BITMAP_TEXT", Pt_ARG_BITMAP_TEXT);
// Widget -> Basic -> Calendar
    PyModule_AddObject(m, "PtCalendar", PyCObject_FromVoidPtr(PtCalendar, NULL));
// Widget -> Basic -> Clock
    PyModule_AddObject(m, "PtClock", PyCObject_FromVoidPtr(PtClock, NULL));
// Widget -> Basic -> Container -> Compound
    PyModule_AddObject(m, "PtCompound", PyCObject_FromVoidPtr(PtCompound, NULL));
// Widget -> Basic -> Container -> Compound -> ComboBox
    PyModule_AddObject(m, "PtComboBox", PyCObject_FromVoidPtr(PtComboBox, NULL));
// Widget -> Basic -> Container -> Compound -> Divider
    PyModule_AddObject(m, "PtDivider", PyCObject_FromVoidPtr(PtDivider, NULL));
// Widget -> Basic -> Container -> Compound -> GenList
    PyModule_AddObject(m, "PtGenList", PyCObject_FromVoidPtr(PtGenList, NULL));
// Widget -> Basic -> Container -> Compound -> GenList -> GenTree
    PyModule_AddObject(m, "PtGenTree", PyCObject_FromVoidPtr(PtGenTree, NULL));
// Widget -> Basic -> Container -> Compound -> GenList -> GenTree -> FileSel
    PyModule_AddObject(m, "PtFileSel", PyCObject_FromVoidPtr(PtFileSel, NULL));
// Widget -> Basic -> Container -> Compound -> GenList -> GenTree -> Tree
    PyModule_AddObject(m, "PtTree", PyCObject_FromVoidPtr(PtTree, NULL));
// Widget -> Basic -> Container -> Compound -> GenList -> List
    PyModule_AddObject(m, "PtList", PyCObject_FromVoidPtr(PtList, NULL));
// Widget -> Basic -> Container -> Compound -> MenuButton
    PyModule_AddObject(m, "PtMenuButton", PyCObject_FromVoidPtr(PtMenuButton, NULL));
// Widget -> Basic -> Container -> Compound -> MultiText
    PyModule_AddObject(m, "PtMultiText", PyCObject_FromVoidPtr(PtMultiText, NULL));
// Widget -> Basic -> Container -> Compound -> Numeric
    PyModule_AddObject(m, "PtNumeric", PyCObject_FromVoidPtr(PtNumeric, NULL));
// Widget -> Basic -> Container -> Compound -> Numeric -> NumericFloat
    PyModule_AddObject(m, "PtNumericFloat", PyCObject_FromVoidPtr(PtNumericFloat, NULL));
// Widget -> Basic -> Container -> Compound -> Numeric -> NumericInteger
    PyModule_AddObject(m, "PtNumericInteger", PyCObject_FromVoidPtr(PtNumericInteger, NULL));
// Widget -> Basic -> Container -> DBContainer
    PyModule_AddObject(m, "PtDBContainer", PyCObject_FromVoidPtr(PtDBContainer, NULL));
// Widget -> Basic -> Container -> FontSel
    PyModule_AddObject(m, "PtFontSel", PyCObject_FromVoidPtr(PtFontSel, NULL));
// Widget -> Basic -> Container -> Group
    PyModule_AddObject(m, "PtGroup", PyCObject_FromVoidPtr(PtGroup, NULL));
// Widget -> Basic -> Container -> Group -> Menu
    PyModule_AddObject(m, "PtMenu", PyCObject_FromVoidPtr(PtMenu, NULL));
// Widget -> Basic -> Container -> Group -> MenuBar
    PyModule_AddObject(m, "PtMenuBar", PyCObject_FromVoidPtr(PtMenuBar, NULL));
// Widget -> Basic -> Container -> Message
    PyModule_AddObject(m, "PtMessage", PyCObject_FromVoidPtr(PtMessage, NULL));
// Widget -> Basic -> Container -> Html
    PyModule_AddObject(m, "PtHtml", PyCObject_FromVoidPtr(PtHtml, NULL));
// Widget -> Basic -> Container -> Pane
    PyModule_AddObject(m, "PtPane", PyCObject_FromVoidPtr(PtPane, NULL));
// Widget -> Basic -> Container -> Pane -> Bkgd
    PyModule_AddObject(m, "PtBkgd", PyCObject_FromVoidPtr(PtBkgd, NULL));
// Widget -> Basic -> Container -> PrintSel
    PyModule_AddObject(m, "PtPrintSel", PyCObject_FromVoidPtr(PtPrintSel, NULL));
// Widget -> Basic -> Container -> Region
    PyModule_AddObject(m, "PtRegion", PyCObject_FromVoidPtr(PtRegion, NULL));
// Widget -> Basic -> Container -> ScrollArea
    PyModule_AddObject(m, "PtScrollArea", PyCObject_FromVoidPtr(PtScrollArea, NULL));
// Widget -> Basic -> Container -> Terminal
    PyModule_AddObject(m, "PtTerminal", PyCObject_FromVoidPtr(PtTerminal, NULL));
// Widget -> Basic -> Container -> Terminal -> Tty
    PyModule_AddObject(m, "PtTty", PyCObject_FromVoidPtr(PtTty, NULL));
// Widget -> Basic -> Container -> UpDown
    PyModule_AddObject(m, "PtUpDown", PyCObject_FromVoidPtr(PtUpDown, NULL));
// Widget -> Basic -> Container -> Window
    PyModule_AddObject(m, "PtWindow", PyCObject_FromVoidPtr(PtWindow, NULL));
    PyModule_AddIntConstant(m, "Pt_ARG_WINDOW_TITLE", Pt_ARG_WINDOW_TITLE);
    PyModule_AddIntConstant(m, "Pt_ARG_WINDOW_TITLE_COLOR", Pt_ARG_WINDOW_TITLE_COLOR);  
    PyModule_AddIntConstant(m, "Pt_ARG_MAX_HEIGHT", Pt_ARG_MAX_HEIGHT);
    PyModule_AddIntConstant(m, "Pt_ARG_MIN_HEIGHT", Pt_ARG_MIN_HEIGHT); 
    PyModule_AddIntConstant(m, "Pt_ARG_MAX_WIDTH", Pt_ARG_MAX_WIDTH);
    PyModule_AddIntConstant(m, "Pt_ARG_MIN_WIDTH", Pt_ARG_MIN_WIDTH);
// Widget -> Basic -> Container -> Window -> Icon
    PyModule_AddObject(m, "PtIcon", PyCObject_FromVoidPtr(PtIcon, NULL));
// Widget -> Basic -> Gauge
    PyModule_AddObject(m, "PtGauge", PyCObject_FromVoidPtr(PtGauge, NULL));
// Widget -> Basic -> Gauge -> Gauge -> Slider
    PyModule_AddObject(m, "PtSlider", PyCObject_FromVoidPtr(PtSlider, NULL));
    PyModule_AddIntConstant(m, "Pt_ARG_SLIDER_LABEL_BR", Pt_ARG_SLIDER_LABEL_BR); 
// Widget -> Basic -> Gauge -> Gauge -> Progress
    PyModule_AddObject(m, "RtProgress", PyCObject_FromVoidPtr(RtProgress, NULL));
// Widget -> Basic -> Graphic
    PyModule_AddObject(m, "PtGraphic", PyCObject_FromVoidPtr(PtGraphic, NULL));
// Widget -> Basic -> Graphic -> Arc
    PyModule_AddObject(m, "PtArc", PyCObject_FromVoidPtr(PtArc, NULL));
// Widget -> Basic -> Graphic -> Bezier
    PyModule_AddObject(m, "PtBezier", PyCObject_FromVoidPtr(PtBezier, NULL));
// Widget -> Basic -> Graphic -> Ellipse
    PyModule_AddObject(m, "PtEllipse", PyCObject_FromVoidPtr(PtEllipse, NULL));
// Widget -> Basic -> Graphic -> Line
    PyModule_AddObject(m, "PtLine", PyCObject_FromVoidPtr(PtLine, NULL));
// Widget -> Basic -> Graphic -> Pixel
    PyModule_AddObject(m, "PtPixel", PyCObject_FromVoidPtr(PtPixel, NULL));
// Widget -> Basic -> Graphic -> Polygon
    PyModule_AddObject(m, "PtPolygon", PyCObject_FromVoidPtr(PtPolygon, NULL));
// Widget -> Basic -> Graphic -> Rect
    PyModule_AddObject(m, "PtRect", PyCObject_FromVoidPtr(PtRect, NULL));
// Widget -> Basic -> Grid
    PyModule_AddObject(m, "Ptgrid", PyCObject_FromVoidPtr(PtGrid, NULL));
// Widget -> Basic -> Label
    PyModule_AddObject(m, "PtLabel", PyCObject_FromVoidPtr(PtLabel, NULL));
    PyModule_AddIntConstant(m, "Pt_ARG_TEXT_FONT", Pt_ARG_TEXT_FONT);
    PyModule_AddIntConstant(m, "Pt_ARG_TEXT_STRING", Pt_ARG_TEXT_STRING);
// Widget -> Basic -> Label -> Button
    PyModule_AddObject(m, "PtButton", PyCObject_FromVoidPtr(PtButton, NULL));
    PyModule_AddIntConstant(m, "Pt_ARG_ARM_COLOR", Pt_ARG_ARM_COLOR);
    PyModule_AddIntConstant(m, "Pt_ARG_ARM_DATA", Pt_ARG_ARM_DATA);
    PyModule_AddIntConstant(m, "Pt_ARG_ARM_FILL", Pt_ARG_ARM_FILL);
// Widget -> Basic -> Label -> Button -> Tab
    PyModule_AddObject(m, "PtTab", PyCObject_FromVoidPtr(PtTab, NULL));
// Widget -> Basic -> Label -> MenuLabel
    PyModule_AddObject(m, "PtMenuLabel", PyCObject_FromVoidPtr(PtMenuLabel, NULL));
// Widget -> Basic -> Label -> Text
    PyModule_AddObject(m, "PtText", PyCObject_FromVoidPtr(PtText, NULL));
// Widget -> Basic -> Label -> ToggleButton
    PyModule_AddObject(m, "PtToggleButton", PyCObject_FromVoidPtr(PtToggleButton, NULL));
// Widget -> Basic -> Label -> OnOffButton
    PyModule_AddObject(m, "PtOnOffButton", PyCObject_FromVoidPtr(PtOnOffButton, NULL));
// Widget -> Basic -> Raw
    PyModule_AddObject(m, "PtRaw", PyCObject_FromVoidPtr(PtRaw, NULL));
// Widget -> Basic -> Scrollbar
    PyModule_AddObject(m, "PtScrollbar", PyCObject_FromVoidPtr(PtScrollbar, NULL));
// Widget -> Basic -> Separator
    PyModule_AddObject(m, "PtSeparator", PyCObject_FromVoidPtr(PtSeparator, NULL));
// Widget -> Basic -> Meter
    PyModule_AddObject(m, "RtMeter", PyCObject_FromVoidPtr(RtMeter, NULL));
// Widget -> Basic -> Trend
    PyModule_AddObject(m, "RtTrend", PyCObject_FromVoidPtr(RtTrend, NULL));
// Widget -> Timer
    PyModule_AddObject(m, "PtTimer", PyCObject_FromVoidPtr(PtTimer, NULL));

    if (PyType_Ready(&PtObject_Type) < 0) return;
    PyModule_AddObject(m, "Widget", (PyObject *)&PtObject_Type);
}
